/**
 * 如果sortCols和compareMethod同时又，优先使用compareMethod.
 * 
 * 
 * @param filename 文件名		
 * @param data 需要导出的数据(js数组)	eg：[{catetory:"酒店",,amount:17625},{catetory:"门票",amount:54625}]
 * @param headers csv表头 			eg: ["品类","点击量"] 
 * @param cols 对应的列名 			eg：["catetory","amount"]
 * @param sortCols 排序列 			eg：["catetory","amount"]  多列需要排序按先后顺序排列
 * @param compareMethod 自定义排序方法（定义比较大小的规则） 		eg：["catetory","amount"] 
 * @return
 */
var exportCsv=function(filename,data,headers,cols,sortCols,compareMethod){
	if(headers.length>cols.length){
		console.log("表头headers和列数cols不一致");
		return;
	}
	if(typeof compareMethod!= "undefined" && typeof compareMethod != "function"){
		return;
	}else if(typeof compareMethod!= "undefined" && typeof compareMethod == "function"){
		data = data.sort(compareMethod);//根据传入的比较方法排序
	}else if(typeof compareMethod == "undefined"){
		if(typeof sortCols === 'object' && !isNaN(sortCols.length)){
			//根据传入排序列合成比较方法
			var compare=function(a,b){
				for(var i=0;i<sortCols.length;i++){
					if(a[sortCols[i]]!=b[sortCols[i]]){
						return a[sortCols[i]] > b[sortCols[i]] ? 1 : -1;
					}
				}
				return 1;
			}
			//排序
			data = data.sort(compare);
		}
	}
	
	var headerCount=headers.length;
	//表头
	var headerString="";
	for(var i=0;i<headerCount;i++){
		headerString +=headers[i]+","
	}
	headerString = headerString.substring(0,headerString.length-1);
	var exportContent="\uFEFF"+headerString+"\n";
	//生产导出内容
	for(var i=0;i<data.length;i++){
		for(var j=0;j<headerCount;j++){
			exportContent += data[i][cols[j]]+",";
		}
		exportContent = exportContent.substring(0,exportContent.length-1)+"\n";
	} 
	var blob = new Blob([exportContent],{type: "text/plain;charset=utf-8"});
	saveAs(blob,  filename+".csv");
}
